<?php
/**
* 用户购物地址管理
*/
namespace V3\Controller;

use Think\Controller; 
use Think\Vender;

class WxpayController extends ApiCommonController{

	
    var $config = array(
        'appid' => "wx937f6f153eaa2d6d",
        'mch_id' => "1300676401",
        'api_key' => "9f900c1fe79839d5796ceeb7e4bfaa82", 
        'notify_url' => 'http://mov30.weixinzjit.com/V3/Wxpay/notify/',
    );

	public function getParm(){
		Vendor("Pay.Wxpay.WxPayHelper");   
		//获取订单数据
		$payid = $_GET['payid']; 
		if(empty($payid)){
			exit(json_encode(array('status'=>"0",'info'=>'ordercode id empty')));
		}
		$orderinfo = $this->getOrderInfo($payid);
		if(!$orderinfo){
			exit(json_encode(array('status'=>"0",'info'=>'get Order Error , Maybe you give the wrong Ordercode')));
		}

		$orderBody = $orderinfo['title'];
		$tade_no = date('Ymd_His',time()).'_'.mt_rand(1000,9999);
		$attach = $payid;
		//$total_fee = 100*$orderinfo['money'];
		$total_fee = 1; 
		$WxPayHelper = new \WxPayHelper($this->config);
		$response = $WxPayHelper->getPrePayOrder($orderBody, $tade_no, $total_fee, $attach);
		$data = $WxPayHelper->getOrder($response['prepay_id']);
 
		//兼容前端为空出问题的情况
		$return_data = array(
			'status'	=>	'1',
			'info'	=>	'参数获取成功',
			'data'	=>	$data,
		);
		$this->ajaxReturn($return_data);
		 
	}

	/**
	* 待完善 支付完成通知接口
	* @return
	*/
	public function notify(){
		Vendor("Pay.Wxpay.WxPayHelper");
		$WxPayHelper = new \WxPayHelper();
		$data = $WxPayHelper->xmlstr_to_array(file_get_contents("php://input"));
		if($data['result_code']=='SUCCESS'){
			$rs = $this->updateOrderStatus($data['attach'],$data['openid'],$data['openid']);
			if($rs){
				echo "success";
			}else{
				echo "fail";
			}
		}else{
			echo "fail";
		}
		exit();
	}
	
	public function _log($data){
		$str = $this->_tostring($data);
		$str = date('Y-m-d H:i:s',time()).'|'.$str.'
';
		$f = fopen('/home/mov30.weixinzjit.com/Public/log/wx_log.txt','a+');
		fwrite($f,$str);
		fclose($f);
	}
	
	public function _tostring($obj){
		if(is_string($obj)) return $obj;
		return json_encode($obj);
	}
	
	
	
	/**
	* 更新订单状态
	* @param $ordercode 订单编号
	* @param $buyer_email 支付者邮箱或者手机号码
	* @param $buyer_id 支付者ID
	* @return
	*/
	function updateOrderStatus($payid,$buyer_email,$buyer_id){
		$orders = M('Order')->where(array('pay_order_id'=>$payid))->select();
		//如果已支付  则返回成功
		foreach($orders as $order){
			if($order['pay_status']==2) continue; 
			$rs = M('Order')->save(array(	'id'=>$order['id'],
											'buyer_email'=>$buyer_email,
											'buyer_id'=>$buyer_id,
											'pay_status'=>2,
											'code_status'=>3));
		}
		return true;
	}
	
	/**
	* 获取订单数据
	* @param undefined $ordercode 
	* @return
	*/
	function getOrderInfo($payid){
		$order = M('Order')->where(array('pay_order_id'=>$payid))->select();
		if(empty($order)) return false;
		$orderinfo =  M('Orderdetial')->where(array('order_id'=>$order[0]['id']))->find();
		if(empty($orderinfo)) return false;
		$money = 0; 
		foreach($order as $od){
			$money += $od['code_money']; 
		}
		
		return array(
			'title'	=>	$orderinfo['product_title'],
			'money'	=>	$money
		);
	}
	
	
}